{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 登录后公钥保存文件\n",
    "## vim ~/.ssh/known_hosts\n",
    "\n",
    "## 登录命令\n",
    "ssh 192.168.1.101 -l test -p 22\n",
    "\n",
    "import pexpect\n",
    "\n",
    "def login_ssh_passwd(port=\"\",user=\"\",host=\"\",passwd=\"\"):\n",
    "    '''函数：用于实现pexepect实现ssh的自动化用户密码登录'''\n",
    "\n",
    "    # print 'ssh -p %s %s@%s' % (port,user, host)\n",
    "    if  port and user and host and passwd:\n",
    "        ssh = pexpect.spawn('ssh -p %s %s@%s' % (port,user, host))\n",
    "        i = ssh.expect(['password:', 'continue connecting (yes/no)?'], timeout=5)\n",
    "        if i == 0 :\n",
    "            ssh.sendline(passwd)\n",
    "        elif i == 1:\n",
    "            ssh.sendline('yes\\n')\n",
    "            ssh.expect('password: ')\n",
    "            ssh.sendline(passwd)\n",
    "        index = ssh.expect ([\"#\", pexpect.EOF, pexpect.TIMEOUT])\n",
    "\n",
    "        if index == 0:\n",
    "            print \"logging in as root!\"\n",
    "            # ssh会话持续交互\n",
    "            ssh.interact()\n",
    "        elif index == 1:\n",
    "            # 子程序退出\n",
    "            print \"logging process exit!\"\n",
    "        elif index == 2:\n",
    "            # 超时\n",
    "            print \"logging timeout exit\"\n",
    "    else:\n",
    "        print \"Parameter error!\"\n",
    "\n",
    "def login_ssh_key(keyfile=\"\",user=\"\",host=\"\",port=\"\"):\n",
    "    '''函数：用于实现pexepect实现ssh的自动化密钥登录'''\n",
    "\n",
    "    if  port and user and host and keyfile:\n",
    "        ssh = pexpect.spawn('ssh -i %s -p %s %s@%s' % (keyfile,port,user, host))\n",
    "        i = ssh.expect( [pexpect.TIMEOUT,'continue connecting (yes/no)?'], timeout=2)\n",
    "        # print '...................................',0\n",
    "        if i == 1:\n",
    "            ssh.sendline('yes\\n')\n",
    "            index = ssh.expect ([\"#\", pexpect.EOF, pexpect.TIMEOUT])\n",
    "        else:\n",
    "            index = ssh.expect ([\"#\", pexpect.EOF, pexpect.TIMEOUT])\n",
    "        if index == 0:\n",
    "            print \"logging in as root!\"\n",
    "            ssh.interact()\n",
    "        elif index == 1:\n",
    "            print \"logging process exit!\"\n",
    "        elif index == 2:\n",
    "            print \"logging timeout exit\"\n",
    "    else:\n",
    "        print \"Parameter error!\"\n",
    "\n",
    "def main():\n",
    "    '''主函数：实现两种方式分别的登录'''\n",
    "    # login_ssh_passwd(port='22',user='root',host='192.168.1.101',passwd='imooccs')\n",
    "    login_ssh_key(keyfile=\"/tmp/id_rsa\",port='22',user='root',host='192.168.1.101')\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## pexpect 模块缺陷\n",
    "1、依赖终端命令的方式\n",
    "2、不同的ssh登录环境兼容较差"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
